package com.sjy.project.ent.api;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.sjy.framework.aspectj.lang.annotation.Log;
import com.sjy.framework.aspectj.lang.enums.BusinessType;
import com.sjy.project.ent.data.service.IEntDataService;
import com.sjy.project.ent.yearSta.domain.EntAmmeter;
import com.sjy.project.ent.yearSta.domain.EntKpiYearSta;
import com.sjy.project.ent.yearSta.domain.EntLandData;
import com.sjy.project.ent.yearSta.domain.EntRentInLand;
import com.sjy.project.ent.yearSta.domain.EntRentOutLand;
import com.sjy.project.ent.yearSta.domain.EntWater;
import com.sjy.project.ent.yearSta.service.IEntAmmeterService;
import com.sjy.project.ent.yearSta.service.IEntKpiYearStaService;
import com.sjy.project.ent.yearSta.service.IEntLandDataService;
import com.sjy.project.ent.yearSta.service.IEntRentInLandService;
import com.sjy.project.ent.yearSta.service.IEntRentOutLandService;
import com.sjy.project.ent.yearSta.service.IEntWaterService;
import com.sjy.project.system.dept.domain.Dept;
import com.sjy.project.system.dept.service.IDeptService;
import com.sjy.project.system.user.domain.User;
import com.sjy.project.system.user.service.IUserService;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import com.sjy.framework.web.controller.BaseController;
import com.sjy.framework.web.domain.AjaxResult;
import com.sjy.common.constant.UserContext;
import com.sjy.common.entity.SecuryUserDetail;
import com.sjy.common.result.BaseResponse;
import com.sjy.common.utils.StringUtils;
import com.sjy.common.utils.poi.ExcelUtil;
import com.sjy.framework.web.page.TableDataInfo;

/**
 * 企业档案Controller
 * 
 * @author ljb
 * @date 2022-06-04
 */
@RestController
@Api(value = "亩均工业企业绩效综合考评接口相关", tags = {"亩均接口"})
@RequestMapping("/api/ent/kpi")
public class ApiEntKpiYearStaController extends BaseController
{

    @Autowired
    private IEntKpiYearStaService kpiYearStaService;

    @Autowired
    private IEntAmmeterService ammeterService;
    @Autowired
    private IEntWaterService waterService;
    @Autowired
    private IEntLandDataService landDataService;
    @Autowired
    private IEntRentInLandService rentInLandService;
    @Autowired
    private IEntRentOutLandService rentOutLandService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IDeptService deptService;
    
	 /**
     * 新增保存企业年度绩效综合考评
     */
    @ApiOperation(value = "301、新增企业年度绩效综合考评", position = 301, notes = "")
//    @ApiImplicitParams({
//    	  @ApiImplicitParam(name = "entName", value = "公司名称", required = false, example = ""),
//    	  @ApiImplicitParam(name = "town", value = "所属乡镇", required = false, example = "海城街道"),
//    	  @ApiImplicitParam(name = "frname", value = "法定代表人", required = false, example = "xx"),
//    	  @ApiImplicitParam(name = "regcap", value = "注册资本", required = false, example = "xx"),
//    	  @ApiImplicitParam(name = "regNo", value = "注册号", required = false, example = "xx"),
//    	  @ApiImplicitParam(name = "creditCode", value = "统代", required = false, example = "xx"),
//    	  @ApiImplicitParam(name = "address", value = "实际办公地址", required = false, example = "林"),
//    	  @ApiImplicitParam(name = "esdate", value = "成立日期", required = false, example = ""),
//    	  @ApiImplicitParam(name = "operateScope", value = "经营范围", required = false, example = "158"),
//    	  @ApiImplicitParam(name = "province", value = "省份", required = false, example = "林"),
//    	  @ApiImplicitParam(name = "city", value = "城市", required = false, example = "林"),
//    	  @ApiImplicitParam(name = "county", value = "县", required = false, example = "林"),
//    	  @ApiImplicitParam(name = "street", value = "镇街", required = false, example = "林"),
//    	  @ApiImplicitParam(name = "dataFrom", value = "数据来源", required = false, example = "林"),
//    	  @ApiImplicitParam(name = "registerTime", value = "企业注册时间", required = false, example = "林"),
//    	  @ApiImplicitParam(name = "industryphy", value = "行业门类", required = false, example = "1"),
//    	  @ApiImplicitParam(name = "contactTel", value = "联系电话", required = false, example = "1"),
//    	  @ApiImplicitParam(name = "joinAssess", value = "是否参评 0参评，1不参评", required = false, example = "1"),
//    	  @ApiImplicitParam(name = "entRule", value = "企业规模:0-规上,1-规下", required = false, example = "1"),
//    	  @ApiImplicitParam(name = "dataYear", value = "数据年份", required = false, example = "1"),
//    	  @ApiImplicitParam(name = "industryName", value = "行业名称", required = false, example = "1"),
//    	  @ApiImplicitParam(name = "regAddress", value = "注册地址", required = false, example = "1"),
//    	  @ApiImplicitParam(name = "entSubType", value = "企业二级类型：1：国有企业，2：民营企业,3：外商独资企业，4: 合资企业 5：港澳台企业", required = false, example = "1"),
//    	  @ApiImplicitParam(name = "entType", value = "企业一级类型：1：企业 2：个体 4：社会组织", required = false, example = "1"),
//    	  @ApiImplicitParam(name = "ammeterNum", value = "用电户号", required = false, example = "1"),
//    	  @ApiImplicitParam(name = "pageNum", value = "当前页", required = false, example = "1"),
//    	  @ApiImplicitParam(name = "pageSize", value = "分页大小", required = false, example = "15"),
//    	  @ApiImplicitParam(name = "orderByColumn", value = "排序字段", required = false, example = "create_time"),
//    	  @ApiImplicitParam(name = "isAsc", value = "排序的方向desc或者asc", required = false, example = "asc"),
//	 })
    @PostMapping("/add")
    @Transactional
    public ResponseEntity<BaseResponse> addSave(@RequestBody EntKpiYearSta entKpiYearSta)
    {
    	if(entKpiYearSta!=null&&entKpiYearSta.getDataYear()==null) {
    		entKpiYearSta.setDataYear(DateUtil.thisYear()-1);
    	}
    	try {
    		//检测是否有重名CreditCode
        	EntKpiYearSta exist = kpiYearStaService.selectEntKpiYearStaByCreditCode(entKpiYearSta.getCreditCode(),entKpiYearSta.getDataYear());
        	if(exist!=null) {
        		return BaseResponse.generateBadResponseEntity("新增失败-当前统代已存在"+entKpiYearSta.getId(), entKpiYearSta);
        	}
        	System.out.println(com.alibaba.fastjson.JSONObject.toJSON(entKpiYearSta));
        	SecuryUserDetail user = UserContext.getUserSession();
        	entKpiYearSta.setCreateBy(user.getLoginName());
        	Date now = new Date();
        	int flag = kpiYearStaService.insertEntKpiYearSta(entKpiYearSta);
        	if(flag>0) {
        		//电费
        		if(entKpiYearSta.getAmmeters()!=null) {
	        		entKpiYearSta.getAmmeters().forEach(n->{
	        			n.setKpiId(entKpiYearSta.getId());
	        			n.setCreateBy(user.getLoginName());
	        			n.setCreditCode(entKpiYearSta.getCreditCode());
	        			n.setDataYear(entKpiYearSta.getDataYear());
	            		ammeterService.insertEntAmmeter(n);
	        		});
        		}
        		//水费
        		if(entKpiYearSta.getWaters()!=null) {
        			entKpiYearSta.getWaters().forEach(n->{
            			n.setKpiId(entKpiYearSta.getId());
            			n.setCreateBy(user.getLoginName());
            			n.setCreditCode(entKpiYearSta.getCreditCode());
            			n.setDataYear(entKpiYearSta.getDataYear());
                		waterService.insertEntWater(n);
            		});
        		}
        		//自有产权证
        		if(entKpiYearSta.getLandDatas()!=null) {
	        		entKpiYearSta.getLandDatas().forEach(n->{
	        			n.setKpiId(entKpiYearSta.getId());
	        			n.setCreateBy(user.getLoginName());
	        			n.setCreditCode(entKpiYearSta.getCreditCode());
	        			landDataService.insertEntLandData(n);
	        		});
        		}
        		//出租土地信息
        		rentInLandService.deleteEntRentInLandByCreditCode(entKpiYearSta.getCreditCode(),entKpiYearSta.getDataYear(),user.getLoginName());
        		if(entKpiYearSta.getRentInLands()!=null) {
	        		entKpiYearSta.getRentInLands().forEach(n->{
		    			n.setId(null);
	        			n.setKpiId(entKpiYearSta.getId());
	        			n.setCreateBy(user.getLoginName());
	        			n.setDataYear(DateUtil.year(now)-1);
	        			n.setCreditCode(entKpiYearSta.getCreditCode());
	        			rentInLandService.insertEntRentInLand(n);
	        			//出租默认插入对方承租信息表--插入的时候需要判断那对方的信息是否已经被街道确定
	        			rentInLandService.copyIn2Out(n);
	        		});
        		}
        		//承租土地信息
        		rentOutLandService.deleteEntRentOutLandByCreditCode(entKpiYearSta.getCreditCode(),entKpiYearSta.getDataYear(),user.getLoginName());
        		if(entKpiYearSta.getRentOutLands()!=null) {
	        		entKpiYearSta.getRentOutLands().forEach(n->{
		    			n.setId(null);
	        			n.setKpiId(entKpiYearSta.getId());
	        			n.setCreateBy(user.getLoginName());
	        			n.setDataYear(DateUtil.year(now)-1);
	        			n.setCreditCode(entKpiYearSta.getCreditCode());
	        			rentOutLandService.insertEntRentOutLand(n);
	        			//承租默认插入对方出租信息表--插入的时候需要判断那对方的信息是否已经被街道确定
	        			rentOutLandService.copyOut2In(n);
	        		});
        		}
        		
            	return BaseResponse.generateOKResponseEntity("新增成功"); 
        	}
        	return BaseResponse.generateBadResponseEntity("新增失败", entKpiYearSta);
		} catch (Exception e) {
			e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return BaseResponse.generateBadResponseEntity("新增失败-数据格式异常",null);
		}
    	
    }
    
    /**
     * 删除企业年度绩效综合考评
     */
    @Log(title = "企业年度绩效综合考评", businessType = BusinessType.DELETE)
    @ApiOperation(value = "302、删除企业年度绩效综合考评", position = 302, notes = "英文半角逗号分割")
    @ApiImplicitParams({
	  @ApiImplicitParam(name = "ids", value = "id ,拼接字符串", required = false, example = "海城"),
		 
	 })
    @PostMapping( "/remove")
    public ResponseEntity<BaseResponse<String>> remove(String ids)
    {
    	int flag = kpiYearStaService.deleteEntKpiYearStaByIds(ids);
    	if(flag>0) {
        	return BaseResponse.generateOKResponseEntity("删除成功"); 
    	}
        return BaseResponse.generateBadResponseEntity("删除失败", ids);
    }
    
    /**
     * 修改保存企业年度绩效综合考评
     */
    @Log(title = "企业年度绩效综合考评", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ApiOperation(value = "303、修改企业年度绩效综合考评", position = 303, notes = "")
    @Transactional
    public ResponseEntity<BaseResponse> editSave( @RequestBody EntKpiYearSta entKpiYearSta)
    {
    	if(entKpiYearSta.getId()==null) {
        	return BaseResponse.generateBadResponseEntity("修改的时候Id不能为空", null);
    	}
    	EntKpiYearSta oldData = kpiYearStaService.selectEntKpiYearStaById(entKpiYearSta.getId());
    	
    	if(oldData.getConfirmFlag()==1) {
        	return BaseResponse.generateBadResponseEntity("当前记录无法修改，因为已于"+DateUtil.formatDate(oldData.getConfirmTime())+"被街道确认！", null);
    	}
    	SecuryUserDetail user = UserContext.getUserSession();
    	entKpiYearSta.setUpdateBy(user.getLoginName());
    	Date now = new Date();
    	int flag = kpiYearStaService.updateEntKpiYearSta(entKpiYearSta);
    	if(flag>0) {
    		//电费--删除再插入
    		ammeterService.deleteEntAmmeterByKpiId(entKpiYearSta.getId(),user.getLoginName());
    		if(entKpiYearSta.getAmmeters()!=null) {
	    		entKpiYearSta.getAmmeters().forEach(n->{
	    			n.setId(null);
	    			n.setKpiId(entKpiYearSta.getId());
	    			n.setCreateBy(user.getLoginName());
	    			n.setCreditCode(entKpiYearSta.getCreditCode());
	    			n.setDataYear(entKpiYearSta.getDataYear());
	        		ammeterService.insertEntAmmeter(n);
	    		});
    		}
    		//水费
    		waterService.deleteEntWaterByKpiId(entKpiYearSta.getId(),user.getLoginName());
    		if(entKpiYearSta.getWaters()!=null) {
	    		entKpiYearSta.getWaters().forEach(n->{
	    			n.setId(null);
	    			n.setKpiId(entKpiYearSta.getId());
	    			n.setCreateBy(user.getLoginName());
	    			n.setCreditCode(entKpiYearSta.getCreditCode());
	    			n.setDataYear(entKpiYearSta.getDataYear());
	        		waterService.insertEntWater(n);
	    		});
    		}
    		//自有产权证
    		landDataService.deleteEntLandDataByKpiId(entKpiYearSta.getId(),user.getLoginName());
    		if(entKpiYearSta.getLandDatas()!=null) {
	    		entKpiYearSta.getLandDatas().forEach(n->{
	    			n.setId(null);
	    			n.setKpiId(entKpiYearSta.getId());
	    			n.setCreateBy(user.getLoginName());
	    			n.setCreditCode(entKpiYearSta.getCreditCode());
	    			landDataService.insertEntLandData(n);
	    		});
    		}
    		//出租土地信息
    		//rentInLandService.deleteEntRentInLandByKpiId(entKpiYearSta.getId(),user.getLoginName());
    		rentInLandService.deleteEntRentInLandByCreditCode(entKpiYearSta.getCreditCode(),entKpiYearSta.getDataYear(),user.getLoginName());
    		if(entKpiYearSta.getRentInLands()!=null) {
	    		entKpiYearSta.getRentInLands().forEach(n->{
	    			n.setId(null);
        			n.setKpiId(entKpiYearSta.getId());
        			n.setCreateBy(user.getLoginName());
        			n.setDataYear(DateUtil.year(now)-1);
        			n.setCreditCode(entKpiYearSta.getCreditCode());
        			rentInLandService.insertEntRentInLand(n);
        			//出租默认插入对方承租信息表
        			rentInLandService.copyIn2Out(n);
        		});
    		}
    		//承租土地信息
    		//rentOutLandService.deleteEntRentOutLandByKpiId(entKpiYearSta.getId(),user.getLoginName());
    		rentOutLandService.deleteEntRentOutLandByCreditCode(entKpiYearSta.getCreditCode(),entKpiYearSta.getDataYear(),user.getLoginName());
    		if(entKpiYearSta.getRentOutLands()!=null) {
	    		entKpiYearSta.getRentOutLands().forEach(n->{
	    			n.setId(null);
        			n.setKpiId(entKpiYearSta.getId());
        			n.setCreateBy(user.getLoginName());
        			n.setDataYear(DateUtil.year(now)-1);
        			n.setCreditCode(entKpiYearSta.getCreditCode());
        			rentOutLandService.insertEntRentOutLand(n);
        			//承租默认插入对方出租信息表
        			rentOutLandService.copyOut2In(n);
        		});
    		}
    		
        	return BaseResponse.generateOKResponseEntity("修改成功"); 
    	}
        return BaseResponse.generateBadResponseEntity("修改失败", null);
    }

    
    @ApiOperation(value = "304、获取企业年度绩效综合考评清单", position = 304, notes = "当前页:pageNum,分页大小：pageSize,排序字段：orderByColumn，排序方式：asc")
//    @ApiImplicitParams({
//      @ApiImplicitParam(name = "id", value = "编号", required = false, example = ""),
//  	  @ApiImplicitParam(name = "entName", value = "公司名称", required = false, example = ""),
//  	  @ApiImplicitParam(name = "town", value = "所属乡镇", required = false, example = "海城街道"),
//  	  @ApiImplicitParam(name = "frname", value = "法定代表人", required = false, example = "xx"),
//  	  @ApiImplicitParam(name = "regcap", value = "注册资本", required = false, example = "xx"),
//  	  @ApiImplicitParam(name = "regNo", value = "注册号", required = false, example = "xx"),
//  	  @ApiImplicitParam(name = "creditCode", value = "统代", required = false, example = "xx"),
//  	  @ApiImplicitParam(name = "address", value = "实际办公地址", required = false, example = "林"),
//  	  @ApiImplicitParam(name = "esdate", value = "成立日期", required = false, example = ""),
//  	  @ApiImplicitParam(name = "operateScope", value = "经营范围", required = false, example = "158"),
//  	  @ApiImplicitParam(name = "province", value = "省份", required = false, example = "林"),
//  	  @ApiImplicitParam(name = "city", value = "城市", required = false, example = "林"),
//  	  @ApiImplicitParam(name = "county", value = "县", required = false, example = "林"),
//  	  @ApiImplicitParam(name = "street", value = "镇街", required = false, example = "林"),
//  	  @ApiImplicitParam(name = "dataFrom", value = "数据来源", required = false, example = "林"),
//  	  @ApiImplicitParam(name = "registerTime", value = "企业注册时间", required = false, example = "林"),
//  	  @ApiImplicitParam(name = "industryphy", value = "行业门类", required = false, example = "1"),
//  	  @ApiImplicitParam(name = "contactTel", value = "联系电话", required = false, example = "1"),
//  	  @ApiImplicitParam(name = "joinAssess", value = "是否参评 0参评，1不参评", required = false, example = "1"),
//  	  @ApiImplicitParam(name = "entRule", value = "企业规模:0-规上,1-规下", required = false, example = "1"),
//  	  @ApiImplicitParam(name = "dataYear", value = "数据年份", required = false, example = "1"),
//  	  @ApiImplicitParam(name = "industryName", value = "行业名称", required = false, example = "1"),
//  	  @ApiImplicitParam(name = "regAddress", value = "注册地址", required = false, example = "1"),
//  	  @ApiImplicitParam(name = "entSubType", value = "企业二级类型：1：国有企业，2：民营企业,3：外商独资企业，4: 合资企业 5：港澳台企业", required = false, example = "1"),
//  	  @ApiImplicitParam(name = "entType", value = "企业一级类型：1：企业 2：个体 4：社会组织", required = false, example = "1"),
//  	  @ApiImplicitParam(name = "ammeterNum", value = "用电户号", required = false, example = "1"),
//  	  @ApiImplicitParam(name = "pageNum", value = "当前页", required = false, example = "1"),
//  	  @ApiImplicitParam(name = "pageSize", value = "分页大小", required = false, example = "15"),
//  	  @ApiImplicitParam(name = "orderByColumn", value = "排序字段", required = false, example = "create_time"),
//  	  @ApiImplicitParam(name = "isAsc", value = "排序的方向desc或者asc", required = false, example = "asc"),
//    })
    @PostMapping({"/list"})
    public ResponseEntity<BaseResponse<TableDataInfo<EntKpiYearSta>>> list(EntKpiYearSta entKpiYearSta)
    {
    	SecuryUserDetail tokenUser = UserContext.getUserSession();
    	User user = userService.selectUserById(tokenUser.getUserId());
    	List<Dept> depts = deptService.selectStreetDepts(user.getDeptId());
    	if(depts==null||depts.size()==0) {
    		return BaseResponse.generateOKResponseEntity(getDataTable(null));
    	}
    	if(depts.size()==1) {
    		entKpiYearSta.setTown(depts.get(0).getDeptName());
    	}
        startPage();
        List<EntKpiYearSta> list = kpiYearStaService.selectEntKpiYearStaList(entKpiYearSta);
        return BaseResponse.generateOKResponseEntity(getDataTable(list));
    }
    
    
    @ApiOperation(value = "305、根据统代跟年份查询详情", position = 305, notes = "")
    @ApiImplicitParams({
  	  @ApiImplicitParam(name = "dataYear", value = "年份", required = false, example = "1"),
  	  @ApiImplicitParam(name = "creditCode", value = "统代", required = false, example = "1")
    })
    @PostMapping({"/getByCreditCodeAndYear"})
    public ResponseEntity<BaseResponse<EntKpiYearSta>> getByCreditCodeAndYear(Integer dataYear,String creditCode) throws Exception
    { 
    	EntKpiYearSta entKpiYearSta = kpiYearStaService.getByCreditCodeAndYear(creditCode,dataYear);
    	if(entKpiYearSta != null) {
        	//详情需要填充 电费 水费 自有产权证 出租 承租信息
    		EntAmmeter entAmmeter = new EntAmmeter();
    		entAmmeter.setKpiId(entKpiYearSta.getId());
    		entAmmeter.setDelFlag("0");
    		entKpiYearSta.setAmmeters(ammeterService.selectEntAmmeterList(entAmmeter));
    		
    		EntWater water = new EntWater();
    		water.setKpiId(entKpiYearSta.getId());
    		water.setDelFlag("0");
    		entKpiYearSta.setWaters(waterService.selectEntWaterList(water));
    		

    		EntLandData landData = new EntLandData();
    		landData.setKpiId(entKpiYearSta.getId());
    		landData.setDelFlag("0");
    		entKpiYearSta.setLandDatas(landDataService.selectEntLandDataList(landData));
    		
    		//承租土地信息
    		EntRentInLand rentInLand = new EntRentInLand();
    		rentInLand.setCreditCode(creditCode);
    		rentInLand.setDataYear(dataYear);
    		rentInLand.setDelFlag("0");
    		entKpiYearSta.setRentInLands(rentInLandService.selectEntRentInLandList(rentInLand));
    		
    		//出租土地信息
    		EntRentOutLand rentOutLand = new EntRentOutLand();
    		rentOutLand.setCreditCode(creditCode);
    		rentOutLand.setDataYear(dataYear);
    		rentOutLand.setDelFlag("0");
    		entKpiYearSta.setRentOutLands(rentOutLandService.selectEntRentOutLandList(rentOutLand));
    		return BaseResponse.generateOKResponseEntity(entKpiYearSta);
    	}
    	return BaseResponse.generateBadResponseEntity(200,"获取企业信息失败", entKpiYearSta);
    	
    }
    
    @ApiOperation(value = "305.2、根据统代跟年份查询详情", position = 305, notes = "测试接口:如果entKpiYearSta还没填写过，生成一个默认值并把rentInLand、rentOutLand给塞进去")
    @ApiImplicitParams({
  	  @ApiImplicitParam(name = "dataYear", value = "年份", required = false, example = "1"),
  	  @ApiImplicitParam(name = "creditCode", value = "统代", required = false, example = "1")
    })
    @PostMapping({"/getByCreditCodeAndYear3"})
    public ResponseEntity<BaseResponse<EntKpiYearSta>> getByCreditCodeAndYear3(Integer dataYear,String creditCode) throws Exception
    { 
    	EntKpiYearSta entKpiYearSta = kpiYearStaService.getByCreditCodeAndYear(creditCode,dataYear);
    	if(entKpiYearSta != null) {
        	//详情需要填充 电费 水费 自有产权证 出租 承租信息
    		EntAmmeter entAmmeter = new EntAmmeter();
    		entAmmeter.setKpiId(entKpiYearSta.getId());
    		entAmmeter.setDelFlag("0");
    		entKpiYearSta.setAmmeters(ammeterService.selectEntAmmeterList(entAmmeter));
    		
    		EntWater water = new EntWater();
    		water.setKpiId(entKpiYearSta.getId());
    		water.setDelFlag("0");
    		entKpiYearSta.setWaters(waterService.selectEntWaterList(water));
    		

    		EntLandData landData = new EntLandData();
    		landData.setKpiId(entKpiYearSta.getId());
    		landData.setDelFlag("0");
    		entKpiYearSta.setLandDatas(landDataService.selectEntLandDataList(landData));
    		
    		//承租土地信息
    		EntRentInLand rentInLand = new EntRentInLand();
    		rentInLand.setCreditCode(creditCode);
    		rentInLand.setDataYear(dataYear);
    		rentInLand.setDelFlag("0");
    		entKpiYearSta.setRentInLands(rentInLandService.selectEntRentInLandList(rentInLand));
    		
    		//出租土地信息
    		EntRentOutLand rentOutLand = new EntRentOutLand();
    		rentOutLand.setCreditCode(creditCode);
    		rentOutLand.setDataYear(dataYear);
    		rentOutLand.setDelFlag("0");
    		entKpiYearSta.setRentOutLands(rentOutLandService.selectEntRentOutLandList(rentOutLand));
    		return BaseResponse.generateOKResponseEntity(entKpiYearSta);
    	}else {
    		entKpiYearSta = new EntKpiYearSta();
    		entKpiYearSta.setDataYear(DateUtil.thisYear()-1);
    		entKpiYearSta.setAmmeters(new ArrayList<EntAmmeter>());
    		entKpiYearSta.setWaters(new ArrayList<EntWater>());
    		entKpiYearSta.setLandDatas(new ArrayList<EntLandData>());
    		//承租土地信息
    		EntRentInLand rentInLand = new EntRentInLand();
    		rentInLand.setCreditCode(creditCode);
    		rentInLand.setDataYear(dataYear);
    		rentInLand.setDelFlag("0");
    		entKpiYearSta.setRentInLands(rentInLandService.selectEntRentInLandList(rentInLand));
    		
    		//出租土地信息
    		EntRentOutLand rentOutLand = new EntRentOutLand();
    		rentOutLand.setCreditCode(creditCode);
    		rentOutLand.setDataYear(dataYear);
    		rentOutLand.setDelFlag("0");
    		entKpiYearSta.setRentOutLands(rentOutLandService.selectEntRentOutLandList(rentOutLand));
    		return BaseResponse.generateOKResponseEntity(entKpiYearSta);
    	}
    	//return BaseResponse.generateBadResponseEntity(200,"获取企业信息失败", entKpiYearSta);
    	
    }
    
    
    @ApiOperation(value = "305、根据id获取场所信息详情", position = 305, notes = "")
    @ApiImplicitParams({
  	  @ApiImplicitParam(name = "id", value = "编号", required = false, example = "1")
    })
    @PostMapping({"/getById"})
    public ResponseEntity<BaseResponse<EntKpiYearSta>> getById(Long id) throws Exception
    { 
    	EntKpiYearSta entKpiYearSta = kpiYearStaService.selectEntKpiYearStaById(id);
    	if(entKpiYearSta != null) {
        	//详情需要填充 电费 水费 自有产权证 出租 承租信息
    		EntAmmeter entAmmeter = new EntAmmeter();
    		entAmmeter.setKpiId(id);
    		entKpiYearSta.setAmmeters(ammeterService.selectEntAmmeterList(entAmmeter));
    		
    		EntWater water = new EntWater();
    		water.setKpiId(id);
    		entKpiYearSta.setWaters(waterService.selectEntWaterList(water));
    		

    		EntLandData landData = new EntLandData();
    		landData.setKpiId(id);
    		entKpiYearSta.setLandDatas(landDataService.selectEntLandDataList(landData));
    		

    		EntRentInLand rentInLand = new EntRentInLand();
    		rentInLand.setKpiId(id);
    		entKpiYearSta.setRentInLands(rentInLandService.selectEntRentInLandList(rentInLand));
    		

    		EntRentOutLand rentOutLand = new EntRentOutLand();
    		rentOutLand.setKpiId(id);
    		entKpiYearSta.setRentOutLands(rentOutLandService.selectEntRentOutLandList(rentOutLand));
    		return BaseResponse.generateOKResponseEntity(entKpiYearSta);
    	}
    	return BaseResponse.generateBadResponseEntity(500,"获取企业信息失败", entKpiYearSta);
    	
    }
    
    @RequiresPermissions("fym:place:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<EntKpiYearSta> util = new ExcelUtil<EntKpiYearSta>(EntKpiYearSta.class);
        return util.importTemplateExcel("企业年度绩效综合考评数据");
    }
    /**
     * 导出企业年度绩效综合考评列表
     */
    @ApiOperation(value = "307、导出企业年度绩效综合考评模板", position = 307, notes = "")
    @Log(title = "企业年度绩效综合考评", businessType = BusinessType.EXPORT)
    @RequestMapping(value="/export", method = {RequestMethod.GET,RequestMethod.POST})
    @ApiImplicitParams({
  })
    public ResponseEntity<BaseResponse<Object>> export( EntKpiYearSta entKpiYearSta,HttpServletResponse response)
    {
        List<EntKpiYearSta> list = kpiYearStaService.selectEntKpiYearStaList(entKpiYearSta);
        ExcelUtil<EntKpiYearSta> util = new ExcelUtil<EntKpiYearSta>(EntKpiYearSta.class);
        //util.exportExcel(list, "企业年度绩效综合考评数据");
        util.exportExcel(response,list, "企业年度绩效综合考评数据");
        return null;
        
        
    }
    
    /**
     * 修改保存企业年度绩效综合考评
     */
    @Log(title = "街道确认企业填报信息", businessType = BusinessType.UPDATE)
    @PostMapping("/confirmByStreet")
    @ApiOperation(value = "308、街道确认企业填报信息准确性", position = 308, notes = "企业更新完填报信息准确性之后企业就不能再对这条信息进行填报了")

    @ApiImplicitParams({
  	  @ApiImplicitParam(name = "id", value = "编号", required = false, example = "1"),
  	  @ApiImplicitParam(name = "confirmFlag", value = "确认状态 1是0否", required = false, example = "1")
    })
    public ResponseEntity<BaseResponse> confirmByStreet(Long id,Integer confirmFlag)
    {
    	EntKpiYearSta kpi = kpiYearStaService.selectEntKpiYearStaById(id);
    	if(kpi == null) {
    		return BaseResponse.generateBadResponseEntity("id无效", null);
    	}
    	SecuryUserDetail user1 = UserContext.getUserSession();
    	User user = userService.selectUserById(user1.getUserId());
    	if(!"00".equals(user.getUserType())||!user.getDept().getDeptName().equals(kpi.getTown())) {
    		return BaseResponse.generateBadResponseEntity("无权修改-当前账号非企业对应街道账号", null);
    	}
    	kpi.setConfirmFlag(confirmFlag);
    	kpi.setConfirmTime(new Date());
    	
    	if(kpiYearStaService.updateEntKpiYearSta(kpi)>0) {
            return BaseResponse.generateOKResponseEntity("修改成功"); 
    	}else {
    		return BaseResponse.generateBadResponseEntity("修改失败", null);
    	}
    }
}
